<?php
/**
 * 巡展基金-项目
 * @author Administrator
 *
 */
class xProjectModel extends MY_Model{
    function xProjectModel(){
        parent::MY_Model();
        $this->table = 'XProject';
    }
    /**
     * 重新组装数据(non-PHPdoc)
     * @see MY_Model::getNewList()
     */
    function getNewList($where = '', $limit = NULL, $offset = NULL, $colum = '', $orderby = ''){
        $findList = parent::getNewList($where, $limit, $offset, $colum, $orderby);
        $list = array();
        foreach ($findList as $v){
            if (isset($v['activityId'])){
                $list[$v['activityId']] = $v;
            }else{
                $list[] = $v;
            }
        }
        return $list;
    }
    /**
     * 更新父类金额(non-PHPdoc)
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->CI->load->model('xunProcessModel');
        $this->xunProcessModel->updateMoney($parentId);
    }
    /**
     * 获取父级关联ID(non-PHPdoc)
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        return isset($data['procId']) && $data['procId'] ? (int)$data['procId'] : 0;
    }
    function packing($list = array()){
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('auditRs');
        foreach ($list as $k => $v){
            //结算审核状态
            $list[$k]['settleAuditStr'] = isset($roleList[$v['settleAuditRole']]) ? $roleList[$v['settleAuditRole']].'已审核' : '';
            if ($list[$k]['settleAuditStr']){
                $list[$k]['settleAuditStr'] .= isset($auditRs[$v['settleAuditRs']]) ? ','.$auditRs[$v['settleAuditRs']] : '';
            }else{
                $list[$k]['settleAuditStr'] = isset($auditRs[$v['settleAuditRs']]) ? $auditRs[$v['settleAuditRs']] : '未审核';
            }
        }
        return $list;
    }
    /**
     * 获取子类金额
     * @param int $activityId
     * @return multitype:number
     */
    function getDetailMoney($activityId){
        //广宣统计金额
        $this->CI->load->model('xPGxModel');
        $gxMoney = $this->CI->xPGxModel->getMoney($activityId);
        //线下活动金额
        $this->CI->load->model('xPOfflineModel');
        $offlineMoney = $this->CI->xPOfflineModel->getMoney($activityId);
        $data['gx'] = array(
            'budgetApply' => $gxMoney['budgetApply'],
            'budgetAudit' => $gxMoney['budgetAudit'],
            'settleApply' => $gxMoney['settleApply'],
            'settleAudit' => $gxMoney['settleAudit'],
        );
        $data['offline'] = array(
            'budgetApply' => $offlineMoney['budgetApply'],
            'budgetAudit' => $offlineMoney['budgetAudit'],
            'settleApply' => $offlineMoney['settleApply'],
            'settleAudit' => $offlineMoney['settleAudit'],
        );
        $data['total'] = array(
            'budgetApply' => $gxMoney['budgetApply'] + $offlineMoney['budgetApply'],
            'budgetAudit' => $gxMoney['budgetAudit'] + $offlineMoney['budgetAudit'],
            'settleApply' => $gxMoney['settleApply'] + $offlineMoney['settleApply'],
            'settleAudit' => $gxMoney['settleAudit'] + $offlineMoney['settleAudit'],
        );
        return $data;
    }
    /**
     * 重新计算金额
     * @param string $activityId
     * @return boolean
     */
    function updateMoney($activityId){
        $childMoney = $this->getDetailMoney($activityId);
        $findData = $this->getData(array('activityId'=>$activityId));
        $data = array(
            'budgetApply' => $childMoney['total']['budgetApply'],
            'settleApply' => $childMoney['total']['settleApply'],
            //'settleAudit' => $childMoney['total']['settleAudit'],
        );
        //结算审核金额居然是这个，靠
        $data['settleAudit'] = in_array($findData['settleAuditRs'], array(1,4)) ? min($childMoney['total']['settleAudit'], (float)$findData['activityCost']) : 0;
        $data['settleMoney'] = $findData['kpi'] * $data['settleAudit'];
        $this->edit(array('activityId'=>$activityId), $data);
    }
    /**
     * 统计对应父级ID的金额
     * @param int $procId 主线流程ID
     * @return multitype:number unknown array
     */
    function getMoney($procId){
        $data = array(
            'fixedMoney'  => 0,//项目定额
            'budgetApply' => 0,//预算申请
            'budgetAudit' => 0,//预算审核
            'settleApply' => 0,//结算申请
            'settleAudit' => 0,//结算审核
            'settleMoney' => 0,//结算金额
        );
        $findList = $this->getList(array('procId' => $procId));
        foreach ($findList as $v){
            $data['fixedMoney']  += $v['activityCost'];//项目定额
            $data['budgetApply'] += $v['budgetApply'];//预算申请
            $data['budgetAudit'] += $v['budgetAudit'];//预算审核
            $data['settleApply'] += $v['settleApply'];//结算申请
            $data['settleAudit'] += $v['settleAudit'];//结算审核
            $data['settleMoney'] += $v['settleMoney'];//结算金额
        }
        $data['siteNum'] = count($findList);
        return $data;
    }
    /**
     * 项目结算一键审核
     * @param int $id
     * @param number $audit
     * @param array $data
     * @return boolean
     */
    function settleOneKeyAudit($activityId, $audit = 1, $user = array()){
        $this->CI->load->model('xPGxModel');
        $this->CI->load->model('xPOfflineModel');
        $this->CI->xPGxModel->settleAuditByActivityId($activityId, $audit, $user);
        $this->CI->xPOfflineModel->settleAuditByActivityId($activityId, $audit, $user);
        $this->edit(array('activityId'=>$activityId), array('settleAuditRole'=>$user['userRole']));
        $this->settleUpdateStatus($activityId);
        //更新金额
        $this->updateMoney($activityId);
        return true;
    }
    /**
     * 更新结算审核状态
     * @param number $id 项目ID
     * @return object
     */
    function settleUpdateStatus($activityId){
        $findData = $this->getData(array('activityId'=>$activityId));
        $data['settleAuditRs'] = $this->getSettleStatus($activityId);
        //$data['settleAuditRs'] == 0 && $data['settleAuditRole'] = 0;
        $this->edit(array('activityId'=>$activityId), $data);
        //同步BTL状态
        $this->CI->load->library('XunSync', NULL, 'XunSync');
        //状态变化是发送状态更改请求
        if ($findData['settleAuditRs'] != $data['settleAuditRs']){
            //0表示未审核，1表示通过，2表示不通过， 3表示部分通过,4表示未提交,5返回修改
            $statusMap = array(-1=>4, 0=>0, 1=>1, 2=>2, 3=>5, 4=>3);
            $status = isset($statusMap[$data['settleAuditRs']]) ? $statusMap[$data['settleAuditRs']] : 0;
            $rs = $this->CI->XunSync->updateSettleStatus($findData['orderId'], $status);
            //记录错误日志
            if ($rs['code'] == 0){
                $this->CI->load->model('xLogModel');
                $this->CI->xLogModel->add(array('typeName'=>'项目状态同步','log'=>'订单ID['.$findData['orderId']."]".$rs['msg'],'createTime'=>date("Y-m-d H:i:s")));
            }
        }
    }
    /**
     * 获取项目结算审核状态值
     * @param string $activityId
     */
    function getSettleStatus($activityId){
        $this->CI->load->model('xPGxModel');
        $this->CI->load->model('xPOfflineModel');
        $gxStatusCount = $this->CI->xPGxModel->getSettleStatusCount($activityId);
        $offlineStatusCount = $this->CI->xPOfflineModel->getSettleStatusCount($activityId);
        $count = array(
            0 => $gxStatusCount[0]+$offlineStatusCount[0],//未审核数
            1 => $gxStatusCount[1]+$offlineStatusCount[1],//通过数
            2 => $gxStatusCount[2]+$offlineStatusCount[2],//不通过数
            3 => $gxStatusCount[3]+$offlineStatusCount[3],//返回修改
        );
        $sumCount = array_sum($count);
        if($sumCount > 0){
            if ($count[0] == $sumCount){
                return 0;//未审核
            }elseif ($count[1] == $sumCount){
                return 1;//通过
            }elseif ($count[2] == $sumCount){
                return 2;//不通过
            }elseif ($count[3] == $sumCount){
                return 3;//返回修改
            }else{
                return 4;//部分通过
            }
        }else{
            return 0;
        }
    }
    /**
     * 获取结算各状态数量
     * @param int $singleId
     * @param number $isBubao
     * @return multitype:number
     */
    function getSettleStatusCount($procId){
        $count = array(
            -1  => 0,//未提交
            0   => 0,//未审核
            1   => 0,//已通过
            2   => 0,//不通过
            3   => 0,//返回修改
            4   => 0,//部分通过
        );
        $findList = $this->getList(array('procId'=>$procId));
        foreach ($findList as $v){
            if (isset($count[$v['settleAuditRs']])){
                $count[$v['settleAuditRs']] ++;
            }
        }
        return $count;
    }
    /**
     * 获取项目费用明细列表
     * @param array $singleInfoList
     */
    function getMoneyDetailList($projectList = array()){
        $initData = array(
            'budgetApply' => array(
                'online'     => 0,
                'offline'  => 0,
                'total'     => 0,
            ),
            'budgetAudit' => array(
                'online'     => 0,
                'offline'  => 0,
                'total'     => 0,
            ),
            'settleApply' => array(
                'online'     => 0,
                'offline'  => 0,
                'total'     => 0,
            ),
            'settleAudit' => array(
                'online'     => 0,
                'offline'  => 0,
                'total'     => 0,
            ),
        );
        $returnList = array();
        //初始化
        foreach ($projectList as $k => $v){
            $returnList[$k] = $initData;
        }
        //广宣列表
        $this->CI->load->model('xPGxModel');
        $gxWhere = array('activityId'=>array('in', array_keys($projectList)));
        $gxList = $this->CI->xPGxModel->getList($gxWhere, NULL, NULL, '', '`activityId` ASC');
        //分析数据
        foreach ($gxList as $v){
            //预算申请
            $returnList[$v['activityId']]['budgetApply']['online'] += $v['budgetApply'];
            $returnList[$v['activityId']]['budgetApply']['total'] += $v['budgetApply'];
            //预算审核
            $returnList[$v['activityId']]['budgetAudit']['online'] += $v['budgetAudit'];
            $returnList[$v['activityId']]['budgetAudit']['total'] += $v['budgetAudit'];
            //结算申请
            $returnList[$v['activityId']]['settleApply']['online'] += $v['settleApply'];
            $returnList[$v['activityId']]['settleApply']['total'] += $v['settleApply'];
            //结算审核
            $returnList[$v['activityId']]['settleAudit']['online'] += $v['settleAudit'];
            $returnList[$v['activityId']]['settleAudit']['total'] += $v['settleAudit'];
        }
    
        //线下列表
        $this->CI->load->model('xPOfflineModel');
        $offlineWhere = array('activityId'=>array('in', array_keys($projectList)));
        $offlineList = $this->CI->xPOfflineModel->getList($offlineWhere, NULL, NULL, '', '`activityId` ASC');
        //分析数据
        foreach ($offlineList as $v){
            //预算申请
            $returnList[$v['activityId']]['budgetApply']['offline'] += $v['budgetApply'];
            $returnList[$v['activityId']]['budgetApply']['total'] += $v['budgetApply'];
            //预算审核
            $returnList[$v['activityId']]['budgetAudit']['offline'] += $v['budgetAudit'];
            $returnList[$v['activityId']]['budgetAudit']['total'] += $v['budgetAudit'];
            //结算申请
            $returnList[$v['activityId']]['settleApply']['offline'] += $v['settleApply'];
            $returnList[$v['activityId']]['settleApply']['total'] += $v['settleApply'];
            //结算审核
            $returnList[$v['activityId']]['settleAudit']['offline'] += $v['settleAudit'];
            $returnList[$v['activityId']]['settleAudit']['total'] += $v['settleAudit'];
        }
        return $returnList;
    }
}